Home = function(){
    var initData = function(){
        Ext.get('chgpwd').on('click',function(){
            alert('chgpwd');
        });
        Ext.get('login').on('click',function(){
            window.location = './?action=unlogin';
        });
        Ext.get('close').on('click',function(){
            window.close();
            return false;
        });
    };
    
    var buildLayout = function(){
        var layout = new Ext.Viewport({
            layout:'border',
            items:[{
                region:'north',
                height:75,
                contentEl:'banner'
            },{
                title:'导航菜单',
                region:'west',
                width:180,
                split:true,
                collapsible:true,
                layout:'accordion',
                defaults:{autoScroll:true,border:false},
                layoutConfig:{
                    animate:true
                }
            },{
                region:'center',
                contentEl:'main-div'
            }]
        });
        
        new Ext.data.Store({
            proxy:new Ext.data.HttpProxy({url:'./?ctrl=menus&action=getMain'}),
            reader:new Ext.data.JsonReader({},['id','title']),
            autoLoad:true
        }).on('load',function(store,records){
            var nav = layout.items.get(1);
            
            for (var i=0; i<records.length; i++){
                var menu = new Ext.Panel({
                    id:records[i].data.id,
                    title:records[i].data.title,
                    iconCls:'nav'
                });
                
                var root = new Ext.tree.AsyncTreeNode({
                    id:records[i].data.id,
                    text:records[i].data.title
                });
                var tree = new Ext.tree.TreePanel({
                    loader:new Ext.tree.TreeLoader({dataUrl:'./?ctrl=menus&action=get&pid='+records[i].data.id}),
                    root:root,
                    border:false,
                    rootVisible:false
                });
                tree.on('click',function(node){
                    if (node.isLeaf()){
                        Ext.get('main').dom.src = node.attributes.url;
                    }
                });
                root.expand(true);
                
                menu.add(tree);
                nav.add(menu);
            }
            layout.doLayout();
        });
    };
    
    return{
        init:function(){
            initData();
            buildLayout();
        }
    }
}();
Ext.onReady(Home.init,Home);